'use strict';
import moment from 'moment';
import '@/assets/js/protoExtend'
import lodash from 'lodash';
import {Message, Loading} from 'element-ui'
let initArrayDay = function (op) {
    let defaultOp = {
        startDay: moment(),
        length: 10
    };
    op = Object.assign({}, defaultOp, op);
    let dArray = [];
    for (let i = 0; i < op.length; i++) {
        let cDay;
        if (i === 0) {
            cDay = op.startDay;
            dArray.push({value: cDay, label: cDay.format('YYYY-MM-DD'), select: true})
        } else {
            cDay = op.startDay.add(1, 'd');
            dArray.push({value: cDay, label: cDay.format('YYYY-MM-DD'), select: false})
        }
    }
    return dArray;
};

let initArrayHour = function () {

    let cHour = moment().hour();
    let hArray = [];
    for (let i = 0; i < 24; i++) {
        if (i < cHour) {
            hArray.push({label: timeRepairZero(i) + ':00', value: i, disabled: false, select: false});
        }
        else if (i === cHour) {
            hArray.push({label: timeRepairZero(i) + ':00', value: i, disabled: false, select: true});
        }
        else {
            hArray.push({label: timeRepairZero(i) + ':00', value: i, disabled: true, select: false});
        }
    }
    return hArray;
};

let reloadArrayHour = function (op) {

    let defaultOp = {
        specificTime: moment(),
        scale: 1,
        start: 0,
        maxDate: moment(),
        minDate: null,
        selectHour: moment().hour()
    };
    op = Object.assign({}, defaultOp, op);
    let hArray = [];
    if (op.maxDate && op.specificTime.format('YYYYMMDD') === op.maxDate.format('YYYYMMDD')) {
        let cHour = op.maxDate.hour();
        for (let i = op.start; i < 24; i = i + op.scale) {
            if (i <= cHour) {
                if (i === op.selectHour) {
                    hArray.push({label: timeRepairZero(i) + ':00', value: i, disabled: false, select: true});
                }
                else {
                    hArray.push({label: timeRepairZero(i) + ':00', value: i, disabled: false, select: false});
                }
            }
            else {
                hArray.push({label: timeRepairZero(i) + ':00', value: i, disabled: true, select: false});
            }
        }
    }
    else {
        for (let i = op.start; i < 24; i = i + op.scale) {
            if (i === op.selectHour) {
                hArray.push({label: timeRepairZero(i) + ':00', value: i, disabled: false, select: true});
            }
            else {
                hArray.push({label: timeRepairZero(i) + ':00', value: i, disabled: false, select: false});
            }
        }
    }
    return hArray;
};

let initArrayMinute = function (op) {

    let defaultOp = {
        timeScale: 5,
        specificTime: moment(),
        selectHour: moment().hour(),
        selectVal: null,  //选中的分钟
        selectFir: false, //选中第一个分钟刻度
        selectLast: false  //选中最后一个分钟刻度
    };

    op = Object.assign({}, defaultOp, op);

    let mArray = [];
    let cDay = moment().format('YYYYMMDD');
    let cHour = moment().hour();
    let cMin = moment().minute();
    let diff = 0;
    for (let i = 0; i < 60; i = i + op.timeScale) {
        if (cDay === op.specificTime.format('YYYYMMDD')) {
            if (cHour === op.selectHour) {
                diff = cMin - i;
                if (diff < 0) {
                    mArray.push({
                        label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                        value: i,
                        disabled: true,
                        select: false
                    });
                }
                else {
                    if (op.selectVal !== null) {
                        if (op.selectVal < cMin && op.selectVal === i) {
                            mArray.push({
                                label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                                value: i,
                                disabled: false,
                                select: true
                            });
                        }
                        else {
                            mArray.push({
                                label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                                value: i,
                                disabled: false,
                                select: false
                            });
                        }
                    }
                    else {
                        if (op.selectFir && i === 0) {
                            mArray.push({
                                label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                                value: i,
                                disabled: false,
                                select: true
                            });
                        }
                        else if (op.selectLast && diff >= 0 && diff < op.timeScale) {
                            mArray.push({
                                label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                                value: i,
                                disabled: false,
                                select: true
                            });
                        }
                        else {
                            mArray.push({
                                label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                                value: i,
                                disabled: false,
                                select: false
                            });
                        }
                    }
                }
            }
            else {
                if (op.selectVal !== null && op.selectVal === i) {
                    mArray.push({
                        label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                        value: i,
                        disabled: false,
                        select: true
                    });
                }
                else if (op.selectFir && i === 0) {
                    mArray.push({
                        label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                        value: i,
                        disabled: false,
                        select: true
                    });
                }
                else if (op.selectLast && i === (60 - op.timeScale)) {
                    mArray.push({
                        label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                        value: i,
                        disabled: false,
                        select: true
                    });
                }
                else {
                    mArray.push({
                        label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                        value: i,
                        disabled: false,
                        select: false
                    });
                }
            }
        }
        else {
            if (op.selectVal !== null && op.selectVal === i) {
                mArray.push({
                    label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                    value: i,
                    disabled: false,
                    select: true
                });
            }
            else if (op.selectFir && i === 0) {
                mArray.push({
                    label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                    value: i,
                    disabled: false,
                    select: true
                });
            }
            else if (op.selectLast && i === (60 - op.timeScale)) {
                mArray.push({
                    label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                    value: i,
                    disabled: false,
                    select: true
                });
            }
            else {
                mArray.push({
                    label: timeRepairZero(op.selectHour) + ":" + timeRepairZero(i),
                    value: i,
                    disabled: false,
                    select: false
                });
            }
        }
    }
    return mArray;
};

let timeRepairZero = function (k) {
    if (k < 10) {
        return '0' + k;
    } else {
        return k;
    }
};

let judgeDate = function (date, n) {
    let diff = moment().format('YYYYMMDD') - date.format('YYYYMMDD');
    return diff > n;
};

let formatGetRequestUri = function (uri, param) {
    let arr = [];
    for (let p in param) {
        if (param[p]) {
            arr.push([p, '=', param[p]].join(''));
        }
    }
    if (uri.indexOf('?') == -1 && arr.length > 0) {
        uri = uri.concat('?');
    }
    if (arr.length > 0) {
        if (uri.charAt(uri.length - 1) !== '?') {
            uri = uri.concat('&');
        }
        uri = uri.concat(arr.join('&'));
    }
    return uri;
};

let showMessage = function (msg, type, duration, onclose) {
    let defaults = {duration: 3000, type: 'error', showClose: true}
    let option
    if (lodash.isPlainObject(msg)) {
        option = msg
    } else {
        option = {
            message: msg,
            type,
            duration,
            onClose: onclose
        }
    }
    if (!option.message) {
        throw Error('[message] is required')
    }
    option = lodash.merge({}, defaults, option)
    return Message(option)
};

let exportDataZ = function (url, param) {
    let form = document.createElement('form');
    form.method = "post";
    form.target = "_self";
    form.action = url;
    form.enctype = "multipart/form-data";
    let str = '';
    if (param) {
        for (let key in param) {
            str += '<input name="' + key + '" value="' + param[key] + '" />';
        }
    }
    form.innerHTML = str;
    form.style.display = 'none';
    document.body.appendChild(form);
    form.submit();
    form.remove();
};

let downLoadProduct = function (url, param) {
    let form = document.createElement('form');
    form.method = "get";
    form.target = "_self";
    form.action = url;
    form.enctype = "multipart/form-data";
    let str = '';
    if (param) {
        for (let key in param) {
            str += '<input name="' + key + '" value="' + param[key] + '" />';
        }
    }
    form.innerHTML = str;
    form.style.display = 'none';
    document.body.appendChild(form);
    form.submit();
    form.remove();
};

let initForArrayHour = function (op) {
    let defaultOp = {
        specificTime: moment(),
        minDate: moment(),
        selectType: 8,
        timeScale: 3,
    };
    op = Object.assign({}, defaultOp, op);
    let hArray = [];
    let num = 2;
    if (op.timeScale === 12) {
        num = 8;
    }
    for (let i = num; i < 24; i = i + op.timeScale) {
        if (op.specificTime.format('YYYYMMDD') === op.minDate.format('YYYYMMDD')) {
            if (op.selectType < i) {
                hArray.push({
                    label: timeRepairZero(i) + ':00',
                    value: i,
                    disabled: false,
                    select: false
                });
            } else if (op.selectType === i) {
                hArray.push({
                    label: timeRepairZero(i) + ':00',
                    value: i,
                    disabled: false,
                    select: true
                });
            } else {
                hArray.push({
                    label: timeRepairZero(i) + ':00',
                    value: i,
                    disabled: true,
                    select: false
                });
            }
        } else {
            if (i === 2) {
                hArray.push({
                    label: timeRepairZero(i) + ':00',
                    value: i,
                    disabled: false,
                    select: true
                });
            } else if (i === 8 && op.timeScale === 12) {
                hArray.push({
                    label: timeRepairZero(i) + ':00',
                    value: i,
                    disabled: false,
                    select: true
                });
            } else {
                hArray.push({
                    label: timeRepairZero(i) + ':00',
                    value: i,
                    disabled: false,
                    select: false
                });
            }
        }
    }

    return hArray;

};

//判断点是否在面内
let judgePointINPolygon = function (point, vs, type){
    let x = point[0], y = point[1];
    let inside = false;
    if(type === 'array'){
        let x = point[0], y = point[1];
        for (let i = 0, j = vs.length - 1; i < vs.length; j = i++) {
            let xi = vs[i][0], yi = vs[i][1];
            let xj = vs[j][0], yj = vs[j][1];
            let intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
            if (intersect) {
                inside = !inside;
            }
        }
    }
    else {
        for (let i = 0, j = vs.length - 1; i < vs.length; j = i++) {
            let xi = vs[i]['lat'], yi = vs[i]['lng'];
            let xj = vs[j]['lat'], yj = vs[j]['lng'];
            let intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
            if (intersect) {
                inside = !inside;
            }
        }
    }
    return inside;
};

let county = [
    {
        name: '江阳区',
        code: 'LZ01',
        lng: 105.445131,
        lat: 28.882889
    },
    {
        name: '龙马潭区',
        code: 'LZ02',
        lng: 105.435228,
        lat: 28.897572
    },
    {
        name: '泸县',
        code: '57508',
        lng: 105.376335,
        lat: 29.151288
    },
    {
        name: '合江县',
        code: '57603',
        lng: 105.834098,
        lat: 28.810325
    },
    {
        name: '纳溪区',
        code: '57604',
        lng: 105.37721,
        lat: 28.77631
    },
    {
        name: '古蔺县',
        code: '57605',
        lng: 105.813359,
        lat: 28.03948
    },
    {
        name: '叙永县',
        code: '57608',
        lng: 105.437775,
        lat: 28.167919
    }
];

let productParam = {

    firstPT : [
        {code: '02', name: '预警'},
        {code: '03', name: '预报'},
    ],

    secondPT: [
        {parentCode: '02', code: '0201', name: '预警信号'},
        {parentCode: '02', code: '0202', name: '风险预警'},
        {parentCode: '02', code: '0203', name: '灾害预警'},
        {parentCode: '03', code: '0301', name: '每日预报'},
        {parentCode: '03', code: '0302', name: '旬报'},
        {parentCode: '03', code: '0303', name: '常规短临预报'},
        {parentCode: '03', code: '0304', name: '灾害短临预报'},
        {parentCode: '03', code: '0305', name: '防灾减灾和地质灾害工作调度会气象信息专报'},
        {parentCode: '03', code: '0306', name: '雨情通报' },
        {parentCode: '03', code: '0307', name: '雨量现报' }
    ],

    thirdPT: [
        {parentCode: '0201', code: '020101', name: '暴雨'},
        {parentCode: '0201', code: '020102', name: '高温'},
        {parentCode: '0201', code: '020103', name: '寒潮'},
        {parentCode: '0201', code: '020104', name: '大风'},
        {parentCode: '0201', code: '020105', name: '冰雹'},
        {parentCode: '0201', code: '020106', name: '雷电'},
        {parentCode: '0201', code: '020107', name: '大雾'},
        {parentCode: '0202', code: '020201', name: '中小河'},
        {parentCode: '0202', code: '020202', name: '山洪'},
        {parentCode: '0202', code: '020203', name: '地灾'},
        {parentCode: '0203', code: '020301', name: '暴雨'},
        {parentCode: '0203', code: '020302', name: '高温'},
        {parentCode: '0203', code: '020303', name: '低温'},
        {parentCode: '0203', code: '020304', name: '大风'},
        {parentCode: '0203', code: '020305', name: '强降温'},
        {parentCode: '0203', code: '020306', name: '寒潮'},
        {parentCode: '0203', code: '020307', name: '大雾'},
        {parentCode: '0203', code: '020308', name: '干旱'},
        {parentCode: '0203', code: '020309', name: '霜冻'},
        {parentCode: '0203', code: '020310', name: '冰冻'},
        {parentCode: '0203', code: '020311', name: '霾'}
    ],

    productType: {

        '020101': '暴雨',
        '020102': '高温',
        '020103': '寒潮',
        '020104': '大风',
        '020105': '冰雹',
        '020106': '雷电',
        '020107': '大雾',

        '020201': '中小河流',
        '020202': '山洪',
        '020203': '地质灾害',
        '02020301': '中小河流',
        '02020302': '滑坡',
        '02020303': '不稳定斜坡',
        '02020304': '崩塌',
        '02020305': '地面沉降',
        '02020306': '地面塌陷',

        '020301': '暴雨',
        '020302': '高温',
        '020303': '低温',
        '020304': '大风',
        '020305': '强降温',
        '020306': '寒潮',
        '020307': '大雾',
        '020308': '干旱',
        '020309': '霜冻',
        '020310': '冰雹',
        '020311': '霾',
    },

    productLevel: [
        {name: '红', code: '0301', color: 'red', visibility: 'visible'},
        {name: '橙', code: '0302', color: 'orange', visibility: 'visible'},
        {name: '黄', code: '0303', color: 'yellow', visibility: 'visible'},
        {name: '蓝', code: '0304', color: 'blue', visibility: 'visible'}
    ],

    warningLevel: {
        '0301': 'red',
        '0302': 'orange',
        '0303': 'yellow',
        '0304': 'blue'
    },

    productTypeLevel: {
        '020101': [0, 1, 2],
        '020102': [0, 1],
        '020103': [0, 1, 2, 3],
        '020104': [0, 1, 2, 3],
        '020105': [0, 1],
        '020106': [0, 1, 2],
        '020107': [0, 1, 2],
        '020201': [0, 1, 2, 3],
        '020202': [0, 1, 2, 3],
        '020203': [0, 1, 2],
        '020301': [0, 1, 2, 3],
        '020302': [1, 2],
        '020303': [2, 3],
        '020304': [1, 2],
        '020305': [1, 2, 3],
        '020306': [1, 2, 3],
        '020307': [1, 2],
        '020308': [1, 2, 3],
        '020309': [3],
        '020310': [1, 2],
        '020311': [3],
    }
};

const NAMESPACE = 'LIAONING';
/**
 * 将key用NAMESPACE包装
 * @param key
 * @private
 */
let _getNamespaceKey = function (key) {
    return [NAMESPACE, key].join('_');
};
/**
 * 向 sessionStorage 存储对象
 * @param key
 * @param value
 */
let setSessionStorage = function (key, value) {
    window.sessionStorage.setItem(
        _getNamespaceKey(key),
        lodash.isPlainObject(value) || lodash.isArray(value) ? JSON.stringify(value) : value
    );
};
/**
 * 获取sessionstorage中存储的对象
 * @param key
 */
let getSessionStorage = function (key) {
    let value = window.sessionStorage.getItem(_getNamespaceKey(key));
    try {
        value = JSON.parse(value);
    } catch (e) {
        // 不是JSON字符串
    }
    return value;
};
/**
 * 从sessionstorage移除对象
 * @param key
 */
let removeSessionStorage = function (key) {
    window.sessionStorage.removeItem(_getNamespaceKey(key));
};
/**
 * 从sessionstorage移除所有
 * @param key
 */
let clearSessionStorage = function (key) {
    window.sessionStorage.clear();
};
export default {
    initArrayDay,
    initArrayHour,
    reloadArrayHour,
    initArrayMinute,
    timeRepairZero,
    judgeDate,
    formatGetRequestUri,
    showMessage,
    exportDataZ,
    downLoadProduct,
    initForArrayHour,
    judgePointINPolygon,
    setSessionStorage,
    getSessionStorage,
    removeSessionStorage,
    clearSessionStorage,

    //数据
    county,

    //产品参数
    productParam
}
